package com.nativetalk.service.study.search;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.study.*;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtReserveDao;
import com.nativetalk.dao.TdNtStudyTeachDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/28.
 */
@Service
public class StudyTeachServiceImpl implements  StudyTeachService {
    private static Logger logger = Logger.getLogger(StudyTeachServiceImpl.class);
    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Override
    public RetInfo findStudyTeachPage(Integer memb_id, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findTdNtMembFoundChangePage:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int totalPage;//总页数
            int totalRecord = tdNtStudyTeachDao.findTdNtStudyTeachByMembIdSize(memb_id);//总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("memb_id",memb_id);
            map.put("currentPage",(currentPage-1)*10);
            map.put("pageSize",pageSize);

            List<TdNtStudyTeachList> tdNtStudyTeachLists = tdNtStudyTeachDao.findTdNtStudyTeachByMembIdPage(map);
            if (tdNtStudyTeachLists == null || tdNtStudyTeachLists.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDYTEACH.RESULT.NULL"));
            } else {
                Map<String, Object> result = new HashMap<>();
                result.put("studyTeachLists", tdNtStudyTeachLists);
                result.put("totalPage", totalPage);
                result.put("totalRecord", totalRecord);
                result.put("currentPage", currentPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取资金变动列表成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findStudyTeachDetail(int stt_id) {
        String logInfo = this.getClass().getName() + ":findStudyTeachDetail:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachDetail tdNtStudyTeachDetail = tdNtStudyTeachDao.findTdNtStudyTeachDetail(stt_id);
            if (tdNtStudyTeachDetail != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("studyTeach", tdNtStudyTeachDetail);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取学习详情成功");
                retInfo.setObj(result);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDYTEACH.RESULT.DETAIL.NULL"));
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findStudyTeach(int stt_id) {
        String logInfo = this.getClass().getName() + ":findStudyTeach:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachToComment tdNtStudyTeachToComment = tdNtStudyTeachDao.findTdNtStudyTeachToComment(stt_id);
            if (tdNtStudyTeachToComment != null) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取信息成功");
                retInfo.setObj(tdNtStudyTeachToComment);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TIPAUTHORERROR"));
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findStudyTeachTeacherPage(Integer tea_id, int currentPage, int pageSize) {
        String logInfo = this.getClass().getName() + ":findStudyTeachTeacherPage:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            int totalPage;//总页数
            int totalRecord = tdNtStudyTeachDao.findTdNtStudyTeachByTeaIdSize(tea_id); //总条数
            if (totalRecord % pageSize == 0) {
                totalPage = totalRecord/pageSize;
            } else {
                totalPage = (totalRecord/pageSize) + 1;
            }
            if (currentPage >= totalPage) {
                currentPage = totalPage;
            }
            if (currentPage <= 1) {
                currentPage = 1;
            }
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("tea_id",tea_id);
            map.put("currentPage",(currentPage-1)*10);
            map.put("pageSize",pageSize);

            List<TdNtStudyTeachTeacherList> tdNtStudyTeachByMembIdPage = tdNtStudyTeachDao.findTdNtStudyTeachByTeaIdPage(map);
            if (tdNtStudyTeachByMembIdPage == null || tdNtStudyTeachByMembIdPage.size() <= 0) {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TEACHER.STUDYTEACH.RESULT.NULL"));
            } else {
                Map<String, Object> result = new HashMap<>();
                result.put("teachingLists", tdNtStudyTeachByMembIdPage);
                result.put("totalPage", totalPage);
                result.put("totalRecord", totalRecord);
                result.put("currentPage", currentPage);
                retInfo.setObj(result);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取学习记录列表成功");
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findStudyTeachTeacherDetail(int stt_id) {
        String logInfo = this.getClass().getName() + ":findStudyTeachTeacherDetail:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachTeacherDetail studyTeach = tdNtStudyTeachDao.findTTdNtStudyTeachTeacherDetail(stt_id);
            if (studyTeach != null) {
                Map<String, Object> result = new HashMap<>();
                result.put("studyTeach", studyTeach);
                result.put("member", Constants.member);
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取学习详情成功.");
                retInfo.setObj(result);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("STUDYTEACH.RESULT.DETAIL.NULL"));
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public TdNtStudyTeachDetail findStudyTeachByTeaId(Integer tea_id) {
        String logInfo = this.getClass().getName() + ":findStudyTeachByTeaId:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        TdNtStudyTeachDetail studyTeach = new TdNtStudyTeachDetail();
        try {
            studyTeach  = tdNtStudyTeachDao.findStudyTeachByTeaId(tea_id);

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return studyTeach;
    }

    @Override
    public int findMembIdBySttId(Integer stt_id) {
        return tdNtStudyTeachDao.findTdNtStudyTeachComment(stt_id).getMemb_id();
    }

    @Resource
    private TdNtReserveDao tdNtReserveDao;

    @Override
    public RetInfo findStudyTeachThree(int reserve_id,Integer memb_id) {
        String logInfo = this.getClass().getName() + ":findStudyTeach:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
           //TdNtStudyTeachCollectComment tdNtStudyTeachCollectComment = tdNtStudyTeachDao.findTdNtStudyTeachToCommentThree(stt_id);
            TdNtCommentPage tdNtReserve=tdNtReserveDao.findCommentById(reserve_id);
            Map<String, Object> obj = new HashMap<String, Object>();
            obj.put("tea_id",tdNtReserve.getTea_id());
            obj.put("memb_id",memb_id);
            //给查询到的老师教师信息添加收藏字段
            String flag = tdNtTeacherDao.foundTdNtTeacherCollection(obj);
            tdNtReserve.setCollection_flag(flag);
            if (tdNtReserve != null) {
                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取信息成功");
                retInfo.setObj(tdNtReserve);
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TIPAUTHORERROR"));
            }
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
